package com.biomatch.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.biomatch.domain.UserInfo;
import com.biomatch.dto.Pagination;
import com.biomatch.response.RestResponse;
import com.biomatch.response.RestResult;
import com.biomatch.service.UserService;
import com.biomatch.utils.Internationalize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("lock.user")
@RequestMapping("/user")
@Api(value="用户管理")
public class UserController {

    @Autowired
    private Internationalize internationalize;

    @Autowired
    private UserService userService;

    @RequestMapping(value="/getUserInfoList", method = RequestMethod.POST)
    @ApiOperation(value = "获得用户信息列表", response = UserInfo.class, httpMethod = "POST")
    public RestResult<List<UserInfo>> getUserList() {
        return RestResponse.success(userService.getUserInfoList());
    }

    @RequestMapping(value="/getUserInfoListAndFingerPrint", method = RequestMethod.POST)
    @ApiOperation(value = "获得用户信息分页列表(指纹)", response = UserInfo.class, httpMethod = "POST")
    public RestResult<UserInfo> getUserInfoListAndFingerPrint(@RequestBody Pagination pagination) {
        IPage<UserInfo>  result=userService.getUserInfoListAndFingerPrint(pagination);
        return RestResponse.success(result);
    }

    @RequestMapping(value = "/getUsersByPage")
    @ApiOperation(value = "获得用户分页列表", response = UserInfo.class, httpMethod = "POST")
    public RestResult<UserInfo> getUsersByPage(@RequestBody Pagination pagination) throws Exception {
        return RestResponse.success(userService.getUsersByPage(pagination));
    }

    @RequestMapping(value="/saveOrUpdateUser", method = RequestMethod.POST)
    @ApiOperation(value = "添加或修改用户", response = UserInfo.class, httpMethod = "POST")
    public RestResult<UserInfo> saveOrUpdateUser(@RequestBody UserInfo userInfo) {
        UserInfo result = userService.saveOrUpdateUser(userInfo);
        if (result == null) {
            return RestResponse.error(internationalize.getMessage("user.name.repeat"));
        } else {
            return RestResponse.success(result);
        }
    }

    @RequestMapping(value="/deleteUser", method = RequestMethod.POST)
    @ApiOperation(value = "删除用户", response = UserInfo.class, httpMethod = "POST")
    public RestResult<UserInfo> deleteUser(@RequestBody UserInfo userInfo) {
        return RestResponse.success(userService.deleteUser(userInfo));
    }
}
